package com.gxa.hualian.controller;

import com.gxa.hualian.mapper.ManagerMapper;
import com.gxa.hualian.pojo.Manager;
import com.gxa.hualian.service.ManagerService;
import com.gxa.hualian.util.JsonResult;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2020/10/22 19:39
 * @Version 1.0
 */
@RestController
@RequestMapping("/mgr")
@Transactional
public class ManagerController {
    @Resource
    ManagerService managerService;
    @Resource
    ManagerMapper managerMapper;

    //添加管理员，通过超级管理员注册管理员
    @PostMapping("/add")
    public Map add(@RequestBody Map<String,String> map){

       return managerService.add(map);
    }

    //管理员登录
    @PostMapping("/login")
    public Map login(@RequestParam("phone") String phone){
        return managerService.login(phone);
    }

    //（超级管理员）启用、冻结管理员账户
    @PostMapping("/updateState")
    public int updateState(@RequestParam("phone") String phone,@RequestParam("state") Integer state){
        return managerService.updateState(phone,state);
    }

    //删除管理员
    @PostMapping("/delete")
    public Map delete(@RequestParam("phone") String phone){
        return managerService.delete(phone);
    }

    //搜索管理员
    @PostMapping("/find")
    public  Map find(@RequestParam("field") String field){
        JsonResult jsonResult  = new JsonResult();
        List<Manager> managers = managerService.find(field);
        jsonResult.setData(managers);
        return jsonResult.getValues();
    }

    //导出管理员列表
    @RequestMapping("/exportTktList")
    public void handle(HttpServletResponse response) throws IOException {
        //获取管理员列表
        List<Manager> managers = managerMapper.selectAll();

        // POI 实现
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("ticket");  // 工作表的名称
        String fileName = "管理员列表.xls";   // 文件名
        // 完美实现中文文件名的转换
        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "编号", "姓名", "电话","注册时间","状态"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int rowNum = 1;
        for (Manager item : managers) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);   // 自增长变量
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getPhone());
            dataRow.createCell(3).setCellValue(dateFormat.format(item.getRegDate()));
            dataRow.createCell(4).setCellValue(item.getState());

            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        // 以附件形式访问
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
 }
